import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class RegisterStudentTest extends AbstractTestCase {

	private static final UniManager um = UniManager.instance;
	private static final CourseFactory cf = CourseFactory.instance;
	private static final StudentFactory sf = StudentFactory.INSTANCE;
	
	private Course c1, c2, c3;
	
	private Student student;
	
	public void setUp() throws Exception {
		Calendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		
		Date now = cal.getTime();

		cal.add(Calendar.DAY_OF_YEAR, -1);
		Date yesterday = cal.getTime();
		
		cal.add(Calendar.DAY_OF_YEAR, -1);
		Date day_before_yesterday = cal.getTime();
		
		cal.add(Calendar.DAY_OF_YEAR, 3);
		Date tomorrow = cal.getTime();
		
		cal.add(Calendar.DAY_OF_YEAR, 1);
		Date day_after_tomorrow = cal.getTime();
		
		c1 = cf.createCourse("c1", now, tomorrow, day_after_tomorrow);
		c2 = cf.createCourse("c2", day_before_yesterday, yesterday, yesterday);
		c3 = cf.createCourse("c4", tomorrow, day_after_tomorrow, day_after_tomorrow);
		
		um.addCourse(c1);
		um.addCourse(c2);
		um.addCourse(c3);
		
		student = sf.addNewStudent(1, "Student1");
	}

	public void tearDown() throws Exception {
	}

	public void test() throws Exception {
		this.setUp();
		System.out.println("--------------------------------------");
		System.out.println("Start of RegisterStudentTest: " + TIME_FORMATTER.format(new Date()));
		System.out.println();
		
		System.out.println(".... Test 1: try registering student 2 times for same course ...................");
		registerStudent(student, c1, 2);
		System.out.println(".... end Test 1 ................................................................");
		
		System.out.println(".... Test 2: try registering student for course after registrationWindow has closed ...................");
		registerStudent(student, c2);
		System.out.println(".... end Test 2 ................................................................");

		System.out.println(".... Test 3: try registering student for course before registrationWindow is open ...................");
		registerStudent(student, c3);
		System.out.println(".... end Test 3 ................................................................");
		
		System.out.println(".... Test 4: unregister student from course ...................");
		unregisterStudent(student, c1);
		System.out.println(".... end Test 4 ................................................................");

		System.out.println(".... Test 5: unregister student from course he is not registered in ...................");
		unregisterStudent(student, c1);
		System.out.println(".... end Test 5 ................................................................");
		
		System.out.println("End of RegisterStudentTest: " + TIME_FORMATTER.format(new Date()));
		System.out.println("--------------------------------------");
		this.tearDown();
	}
	
	private void unregisterStudent(Student student, Course course) {
		System.out.println("Unregister student " + student.getMatrikelNr() + " from course " + course.getCourseName());
		System.out.println("Course Details: " + AddNewCourseTest.getCourseDetails(course));
		System.out.println("Currently registered students for course: " + course.getNumberOfRegistrations());
		
		try {
			um.unregisterStudent(course, student);
			System.out.println("Successfully unregistered student. Number of registrations in course: " + course.getNumberOfRegistrations());
		} catch(Exception e) {
			System.out.println("Could not unregister student: " + e.getMessage());
		}
		System.out.println();
	}
	
	private void registerStudent(Student student, Course course) {
		registerStudent(student, course, 1);
	}
	
	private void registerStudent(Student student, Course course, Integer numberOfRegisters) {
		if(numberOfRegisters == null || numberOfRegisters < 1) {
			numberOfRegisters = 1;
		}
		
		for(int i = 0; i < numberOfRegisters; i++) {
			System.out.println("Register student " + student.getMatrikelNr() + " for Course " + course.getCourseName());
			System.out.println("Course Details: " + AddNewCourseTest.getCourseDetails(course));
			System.out.println("Currently registered students for course: " + course.getNumberOfRegistrations());
			try {
				um.registerStudent(course, student);
				System.out.println("Successfully registered student to course. Number of registrations in course: " + course.getNumberOfRegistrations());
			} catch (Exception e) {
				System.out.println("Could not register student: " + e.getMessage());
			}
			System.out.println();
		}
	}
}
